home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Sound / SndPlayDoubleBuffer / _source / MungeBuffer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-15  |  4.6 KB  |  155 lines  |  [TEXT/CWIE]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Routines demonstrating how to manipulate buffers of WAVE data to get them to play correctly.
  5. **
  6. **    by Mark Cookson, Apple Developer Technical Support
  7. **
  8. **    File:    MungeBuffer.c
  9. **
  10. **    Copyright ©1996 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "Apple Sample
  17. **    Code" after having made changes. If you're going to re-distribute the
  18. **    source, we require that you make it clear in the source that the code
  19. **    was descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. #include "MungeBuffer.h"
  23.  
  24. /* This PowerPC assembly routine is used to make a buffer of WAVE sound
  25.    data play as if it was a buffer of a AIFF sound data.
  26.  
  27.    All this really means is that you have to do the endian conversion without
  28.    rearanging the channels if the sound is stereo.
  29.  
  30.    The buffer addressed is passed in register r3 and the count is passed in r4.
  31. */
  32. #ifdef ASM    // Should we use the assembly versions or the C versions of the buffer munging routines?
  33.  
  34. #ifdef powerc
  35.  
  36. asm void Endian16BitBuffer (Ptr buf, unsigned long count)
  37. {
  38.         b        test                        // Make sure count is good
  39. more:    subi    r4, r4, 0x04                // Move pointer back 4 bytes to next long
  40.         lwzx    r5, r3, r4                    // Load the long
  41.         rlwinm    r5, r5, 0x10, 0x00, 0x1F    // Rotate long left 16 bits
  42.         stwbrx    r5, r3, r4                    // Write it back out byte reversed
  43. test:    cmpwi    r4, 0x03                    // Are we done yet?
  44.         bgt        more                        // Go back to process more longs
  45.         blr                                    // We're outta here
  46. }
  47.  
  48. /* These are the coresponding 68K versions of the above PowerPC assembly buffer munging routines.
  49.  
  50.    These calls will be called if the target is a 68K based Mac.
  51.  
  52.    The buffer address is passed as the second value (from the top) of the stack, and the count
  53.    is the third value (from the top) of the stack.
  54. */
  55.  
  56. #else //generating 68K
  57.  
  58. asm void Endian16BitBuffer (Ptr buf, unsigned long count)
  59. {
  60.         movea.l    0x04(sp), a0                // Put address of buffer in a0
  61.         move.l    0x08(sp), d0                // Put count in d0
  62.         lsr.l    #0x02, d0                    // Divide count by 4
  63.         bra        test                        // Make sure count is good
  64. more:    move.l    (a0), d1                    // Load the long
  65.         ror.w    #0x08, d1                    // Rotate bottom 16 bits
  66.         swap    d1                            // Swap upper and lower words of d1
  67.         ror.w    #0x08, d1                    // Rotate bottom 16 bits (used to be top bits)
  68.         swap    d1                            // Swap upper and lower words of d1
  69.         move.l    d1, (a0)+                    // Store the result
  70. test:    dbra    d0, more                    // Go back and process more longs
  71.         rts                                    // We're outta here
  72. }
  73.  
  74. #endif
  75.  
  76. #else
  77.  
  78. /* Use C functions instead of assembly functions.
  79.    To use these functions, undefine ASM in MungeBuffer.h.
  80. */
  81.  
  82. void Endian16BitBuffer (Ptr buf, unsigned long count)
  83. {
  84.     unsigned long    i;
  85.     unsigned char    tempChar;
  86.  
  87.     for (i = kStartOfBuffer; i < count; i += sizeof (short)) {
  88.         tempChar = (buf)[i];
  89.         (buf)[i] = (buf)[i+1];
  90.         (buf)[i+1] = tempChar;
  91.     }
  92. }
  93.  
  94. #endif
  95.  
  96. void ReverseMono8BitBuffer (const Ptr buf, unsigned long count)
  97. {
  98.     unsigned char    *endBuffer;
  99.     unsigned char    *startBuffer = (unsigned char *)buf;
  100.     unsigned char    saveByte;
  101.     long            i;
  102.  
  103.     endBuffer = ((unsigned char *)buf + count) - 1;
  104.     for (i = (count / (sizeof (char) * kHalfOfBuffer)); i >= kStartOfBuffer; --i) {
  105.         saveByte = *endBuffer;
  106.         *endBuffer-- = *startBuffer;
  107.         *startBuffer++ = saveByte;
  108.     }
  109. }
  110.  
  111. void ReverseStereo8BitBuffer (const Ptr buf, unsigned long count)
  112. {
  113.     unsigned short    *endBuffer;
  114.     unsigned short    *startBuffer = (unsigned short *)buf;
  115.     unsigned short    saveShort;
  116.     long            i;
  117.  
  118.     endBuffer = (unsigned short *)(buf + count) - 1;
  119.     for (i = (count / (sizeof (short) * kHalfOfBuffer)); i >= kStartOfBuffer; --i) {
  120.         saveShort = *endBuffer;
  121.         *endBuffer-- = *startBuffer;
  122.         *startBuffer++ = saveShort;
  123.     }
  124. }
  125.  
  126. void ReverseMono16BitBuffer (const Ptr buf, unsigned long count)
  127. {
  128.     unsigned short    *endBuffer;
  129.     unsigned short    *startBuffer = (unsigned short *)buf;
  130.     unsigned short    saveShort;
  131.     long            i;
  132.  
  133.     endBuffer = (unsigned short *)(buf + count) - 1;
  134.     for (i = (count / (sizeof (short) * kHalfOfBuffer)); i >= kStartOfBuffer; --i) {
  135.         saveShort = *endBuffer;
  136.         *endBuffer-- = *startBuffer;
  137.         *startBuffer++ = saveShort;
  138.     }
  139. }
  140.  
  141. void ReverseStereo16BitBuffer (const Ptr buf, unsigned long count)
  142. {
  143.     unsigned long    *endBuffer;
  144.     unsigned long    *startBuffer = (unsigned long *)buf;
  145.     unsigned long    saveLong;
  146.     long            i;
  147.  
  148.     endBuffer = (unsigned long *)(buf + count) - 1;
  149.     for (i = (count / (sizeof (long) * kHalfOfBuffer)); i >= kStartOfBuffer; --i) {
  150.         saveLong = *endBuffer;
  151.         *endBuffer-- = *startBuffer;
  152.         *startBuffer++ = saveLong;
  153.     }
  154. }
  155.